package com.asktgapp.engineer.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.engineer.activity.MyAnswerActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * time:2018/1/24
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class QuestionFragment extends BaseFragment {

    @InjectView(R.id.viewpager)
    ViewPager viewPager;
    @InjectView(R.id.group)
    RadioGroup mRadioGroup;
    @InjectView(R.id.right)
    RadioButton right;

    @InjectView(R.id.myBack)
    TextView myBack;
    @InjectView(R.id.myToolbarRight)
    TextView myToolbarRight;
    private MyPagerAdapter pagerAdapter;
    private int position;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragmenet_question, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().getToolbar().setVisibility(View.GONE);
//        setHasOptionsMenu(true);
        position = getActivity().getIntent().getIntExtra("positon", 0);
        pagerAdapter = new MyPagerAdapter(getFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(position);
        viewPager.setOffscreenPageLimit(2);
        myBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBaseActivity().onBackPressed();
            }
        });
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                int position = group.indexOfChild(group.findViewById(checkedId));
                viewPager.setCurrentItem(position);
            }
        });
        if (position == 1) {
            right.setChecked(true);
        }
        myToolbarRight.setVisibility(View.VISIBLE);
        myToolbarRight.setText("我的");
        myToolbarRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyAnswerActivity.class));
            }
        });
    }

    @Override
    public void loadData() {

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return QuestionItemFragment.newInstance(3);
                case 1:
                    return QuestionItemFragment.newInstance(4);
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

}
